﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.UICommon;
using ESOL.WinUI.MasterData;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageCustomer : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstCustomerEntity mSelectdeCustomer;
        #endregion

        #region Constructor
        public frmManageCustomer()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BingCombobox()
        {
            UICommonFunctions.BindComboboxByCommonItem(cmbCustomerLevel, "CL", true);
            UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(cLevel, "CL", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(DefaultCurrency, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(CustomerStatus, "STATUS", true);

            DataTable dt = MstIndustryTypeBs.GetAllIndustryType();
            DataRow dr = dt.NewRow();
            dr["INDUSTRY_TYPE_CODE"] = "";
            dt.Rows.InsertAt(dr, 0);
            cmbCustomerType.DataSource = dt;
            cmbCustomerType.DisplayMember = "INDUSTRY_TYPE_NAME";
            cmbCustomerType.ValueMember = "INDUSTRY_TYPE_CODE";

            cLevel.NullText = "";
            DefaultCurrency.NullText = "";
            CustomerStatus.NullText = "";
            cmbStatus.SetSelectedValue("STATUS03");
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtCustomerCode.Text) == false)
            {
                condition += " AND D.CUSTOMER_CODE LIKE '%" + txtCustomerCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtCoustomerName.Text) == false)
            {
                condition += " AND D.CUSTOMER_NAME LIKE '%" + txtCoustomerName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProvince.Text) == false)
            {
                condition += " AND D.PROVINCE LIKE '%" + txtProvince.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbCustomerLevel.Text) == false && cmbCustomerLevel.SelectedValue != null)
            {
                condition += " AND D.LEVEL = '" + cmbCustomerLevel.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbCustomerType.Text) == false && cmbCustomerType.SelectedValue != null)
            {
                condition += " AND D.INDUSTRY_TYPE_CODE = '" + cmbCustomerType.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND D.STATUS='" + cmbStatus.SelectedValue.ToString() + "'";
            }
            else
            {
                condition += " AND D.STATUS!='STATUS05'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "INDUSTRY_TYPE_CODE";

            DataTable dt =  MstCustomerBs.GetCustomerByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void SendMessage(string sMessageTitle,string receivedPerson,string sMessageBody,string MessageLevel)
        {
            MstMessageEntity message = new MstMessageEntity();
            message.MessageId = Guid.NewGuid().ToString();
            message.MessateTitle = sMessageTitle;
            message.MessageSender = CommonParameter.CurrentUser.PersonId;
            message.MessageBody = sMessageBody;
            message.MessageLevel = MessageLevel;
            message.MessageDate = DateTime.Now;

            MstPersonEntity person = MstPersonBs.GetMstPersonByID(receivedPerson);
            List<MstPersonEntity> toList = new List<MstPersonEntity>();
            toList.Add(person);

            MstMessageBs.SendMessage(message, toList);
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BingCombobox();
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectdeCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.View;
            frm.selectedCustomer = mSelectdeCustomer;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectdeCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中客户？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                //mSelectedCommonItem.IsDeleted = 1;
                mSelectdeCustomer.Status = "STATUS05";
                bool successful = MstCustomerBs.UpdateMstCustomer(mSelectdeCustomer);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectdeCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.Modify;
            frm.selectedCustomer = mSelectdeCustomer;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Approve()
        {
            if (mSelectdeCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectdeCustomer.Status != "STATUS02")
            {
                MessageBox.Show("该客户当前状态不可审核！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCustomer frm = new frmMaintainCustomer();
            frm.ThisActionType = ActionType.Approve;
            frm.selectedCustomer = mSelectdeCustomer;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Submit()
        {
            if (mSelectdeCustomer == null)
            {
                MessageBox.Show("请选择一个客户！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectdeCustomer.Status != "STATUS01")
            {
                MessageBox.Show("该客户当前状态不可送审！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否送审选中客户？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectdeCustomer.Status = "STATUS02";
                bool successful = MstCustomerBs.UpdateMstCustomer(mSelectdeCustomer);
                if (successful)
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    SendMessage("测试一下", CommonParameter.CurrentUser.PersonId, "这是在测试审核客户资料建立流程中发送的要求审核的信息：借记卡号大家爱上打开哈空间好看的哈哈是的哈sdkhas结婚的卡上按键速度很快将按实际的机会", MessageLevel.ML03.ToString());
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Export()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(sfd.FileName);
            }
            //base.Click_Export();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectdeCustomer = new MstCustomerEntity(dr);
                 if (mSelectdeCustomer.Status == "STATUS01")
                {
                    btnSubmit.Enabled = true;
                    btnApprove.Enabled = false;
                    btnModify.Enabled = true;
                    btnDelete.Enabled = true;
                }
                else if (mSelectdeCustomer.Status == "STATUS02")
                {
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = true;
                    btnModify.Enabled = false;
                    btnDelete.Enabled = true;
                }
                else if (mSelectdeCustomer.Status == "STATUS03")
                {
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnModify.Enabled = false;
                    btnDelete.Enabled = true;
                }
                else if (mSelectdeCustomer.Status == "STATUS04")
                {
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnModify.Enabled = true;
                    btnDelete.Enabled = true;
                }
                else if (mSelectdeCustomer.Status == "STATUS05")
                {
                    btnSubmit.Enabled = false;
                    btnApprove.Enabled = false;
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                }
        }

        private void txtProvince_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAreaQuery frm = new frmMaintainAreaQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedArea != null)
            {
                txtProvince.Text = frm.SelectedArea.AreaName;
                txtProvince.Tag = frm.SelectedArea;
            }
            else
            {
                txtProvince.Text = "";
                txtProvince.Tag = null;
            }
        }
        #endregion

    }
}
